Support attr_reader/attr_writer/attr_accessor in singleton classes#463
Merged
Conversation
6e30078 to
fcec0a8
Compare
`attr_reader`, `attr_writer`, and `attr_accessor` written inside a
singleton class (e.g. `class << self`) were always treated as instance
methods, so
class C
class << self
attr_accessor :age
end
end
was dumped as `def age` / `def age=` instead of `def self.age` /
`def self.age=`.
The three attr meta nodes ignored the enclosing scope and hardcoded
`false` for the singleton flag when resolving the backing ivar and
defining the methods. Detect the metaclass scope the same way `DefNode`
does (`lenv.cref.scope_level == :metaclass`) and thread it through, so
the accessors and their ivar are defined on the singleton. This also
covers `class << SomeConstant`.
Promote scenario/known-issues/eigenclass-attr.rb to scenario/class/.
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
fcec0a8 to
8d282ed
Compare
sinsoku
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
attr_reader/attr_writer/attr_accessorwritten inside a singleton class (class << self) were treated as instance methods:was inferred as
def age/def age=instead ofdef self.age/def self.age=.Changes
lenv.cref.scope_level == :metaclasscheckDefNodealready uses — and define the accessors and their backing ivar on the singleton. This also coversclass << SomeConstant.scenario/known-issues/eigenclass-attr.rbtoscenario/class/.All 433 tests pass (
bundle exec rake test).